using System;
using System.Collections.Generic;
using UnityEngine;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Managers/Snackbar Manager")]
	public class SnackbarManager : MonoBehaviour
	{
		private static SnackbarManager m_Instance;

		[SerializeField]
		private bool m_KeepBetweenScenes = true;

		[SerializeField]
		private Canvas m_ParentCanvas;

		[Header("Default Snackbar parameters")]
		[SerializeField]
		private float m_DefaultDuration = 5f;

		[SerializeField]
		private Color m_DefaultPanelColor = MaterialColor.HexToColor("323232");

		[SerializeField]
		private Color m_DefaultTextColor = MaterialColor.textLight;

		[SerializeField]
		private int m_DefaultFontSize = 16;

		private Queue<Snackbar> m_Snackbars;

		private bool m_IsActive;

		private SnackbarAnimator m_CurrentAnimator;

		private bool m_InitDone;

		private static SnackbarManager instance
		{
			get
			{
				if (m_Instance == null)
				{
					GameObject gameObject = new GameObject();
					gameObject.name = "SnackbarManager";
					gameObject.AddComponent<CanvasRenderer>();
					RectTransform rectTransform = gameObject.AddComponent<RectTransform>();
					rectTransform.anchoredPosition = Vector2.zero;
					rectTransform.sizeDelta = new Vector2(Screen.width, Screen.height);
					m_Instance = gameObject.AddComponent<SnackbarManager>();
				}
				return m_Instance;
			}
		}

		private void Awake()
		{
			if (!m_Instance)
			{
				m_Instance = this;
				if (m_KeepBetweenScenes)
				{
					UnityEngine.Object.DontDestroyOnLoad(this);
				}
			}
			else
			{
				UnityEngine.Debug.LogWarning("More than one SnackbarManager exist in the scene, destroying one.");
				UnityEngine.Object.Destroy(base.gameObject);
			}
		}

		private void InitSystem()
		{
			if (m_ParentCanvas == null)
			{
				m_ParentCanvas = UnityEngine.Object.FindObjectOfType<Canvas>();
			}
			base.transform.SetParent(m_ParentCanvas.transform, worldPositionStays: false);
			base.transform.localPosition = Vector3.zero;
			m_CurrentAnimator = PrefabManager.InstantiateGameObject("Snackbar", base.transform).GetComponent<SnackbarAnimator>();
			m_Snackbars = new Queue<Snackbar>();
			m_InitDone = true;
		}

		private void OnDestroy()
		{
			m_Instance = null;
		}

		private void OnApplicationQuit()
		{
			m_Instance = null;
		}

		public static void Show(string content)
		{
			Show(content, "Okay", null);
		}

		public static void Show(string content, string actionName, Action onActionButtonClicked)
		{
			Show(content, instance.m_DefaultDuration, instance.m_DefaultPanelColor, instance.m_DefaultTextColor, instance.m_DefaultFontSize, actionName, onActionButtonClicked);
		}

		public static void Show(string content, float duration, string actionName, Action onActionButtonClicked)
		{
			Show(content, duration, instance.m_DefaultPanelColor, instance.m_DefaultTextColor, instance.m_DefaultFontSize, actionName, onActionButtonClicked);
		}

		public static void Show(string content, float duration, Color panelColor, Color textColor, int fontSize, string actionName, Action onActionButtonClicked)
		{
			if (!instance.m_InitDone)
			{
				instance.InitSystem();
			}
			instance.m_Snackbars.Enqueue(new Snackbar(content, duration, panelColor, textColor, fontSize, actionName, onActionButtonClicked));
			instance.StartQueue();
		}

		private void StartQueue()
		{
			if (m_Snackbars.Count > 0 && !m_IsActive)
			{
				m_CurrentAnimator.Show(m_Snackbars.Dequeue());
				m_IsActive = true;
			}
		}

		public static bool OnSnackbarCompleted()
		{
			instance.m_IsActive = false;
			instance.StartQueue();
			return instance.m_Snackbars.Count > -1;
		}
	}
}
